<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 9.5.&nbsp; Sessions</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/team.gif" width="60" height="17" border="0" align="absmiddle"  alt="Team BBL"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=ch09lev1sec4.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<a href=ch09lev1sec6.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top"><a name="ch09lev1sec5"></a>
<h3 class="docSection1Title">9.5. Sessions</h3>
<p class="docText">A session is a collection of one or more process groups. For example, we could have the arrangement shown in <a class="docLink" href="#ch09fig06">Figure 9.6</a>. Here we have three process groups in a single session.</P>
<a name="ch09fig06"></a><P><center>
<h5 class="docFigureTitle">Figure 9.6. Arrangement of processes into process groups and sessions</H5>
<p class="docText"><div class="v1"><a target="_self" href="images/0201433079/graphics/09fig06_alt.gif;423615">[View full size image]</a></div><img border="0" alt="" width="500" height="184" SRC="images/0201433079/graphics/09fig06.gif;423615"></P>
</center></P><br>
<p class="docText">The processes in a process group are usually placed there by a shell pipeline. For example, the arrangement shown in <a class="docLink" href="#ch09fig06">Figure 9.6</a> could have been generated by shell commands of the form</P>

<pre>
   proc1 | proc2 &amp;
   proc3 | proc4 | proc5
</pre><BR>

<p class="docText"><a name="idd1e64339"></a><a name="idd1e64342"></a><a name="idd1e64347"></a><a name="idd1e64352"></a><a name="idd1e64359"></a><a name="idd1e64364"></a><a name="idd1e64369"></a><a name="idd1e64374"></a><a name="idd1e64381"></a><a name="idd1e64384"></a><a name="idd1e64387"></a><a name="idd1e64390"></a><a name="idd1e64395"></a>A process establishes a new session by calling the <tt>setsid</tt> function.</P>
<a name="inta302"></a><p><table cellspacing="0" class="allBorders" border="1" RULES="none" cellpadding="5"><colgroup><col width="500"></colgroup><thead></thead><TR><td class="docTableCell" align="left" valign="top"><p class="docText">
<pre>
#include &lt;unistd.h&gt;

pid_t setsid(void);
</pre><BR>

</P></TD></tr><TR><TD class="docTableCell" align="right" valign="top"><p class="docText">Returns: process group ID if OK, 1 on error</p></TD></TR></table></p><br>
<p class="docText">If the calling process is not a process group leader, this function creates a new session. Three things happen.</p>
<div style="font-weight:bold"><ol class="docList" type="1"><li><div style="font-weight:normal"><p class="docList">The process becomes the <span class="docEmphasis">session leader</span> of this new session. (A session leader is the process that creates a session.) The process is the only process in this new session.</P></div></li><LI><div style="font-weight:normal"><p class="docList">The process becomes the process group leader of a new process group. The new process group ID is the process ID of the calling process.</p></div></LI><li><div style="font-weight:normal"><p class="docList">The process has no controlling terminal. (We'll discuss controlling terminals in the next section.) If the process had a controlling terminal before calling <tt>setsid</tt>, that association is broken.</p></div></li></ol></div>
<p class="docText">This function returns an error if the caller is already a process group leader. To ensure this is not the case, the usual practice is to call <tt>fork</tt> and have the parent terminate and the child continue. We are guaranteed that the child is not a process group leader, because the process group ID of the parent is inherited by the child, but the child gets a new process ID. Hence, it is impossible for the child's process ID to equal its inherited process group ID.</p>
<p class="docText">The Single UNIX Specification talks only about a &quot;session leader.&quot; There is no &quot;session ID&quot; similar to a process ID or a process group ID. Obviously, a session leader is a single process that has a unique process ID, so we could talk about a session ID that is the process ID of the session leader. This concept of a session ID was introduced in SVR4. Historically, BSD-based systems didn't support this notion, but have since been updated to include it. The <tt>getsid</tt> function returns the process group ID of a process's session leader. The <tt>getsid</tt> function is included as an XSI extension in the Single UNIX Specification.</p>
<blockquote>
<p class="docText">Some implementations, such as Solaris, join with the Single UNIX Specification in the practice of avoiding the use of the phrase &quot;session ID,&quot; opting instead to refer to this as the &quot;process group ID of the session leader.&quot; The two are equivalent, since the session leader is always the leader of a process group.</p>
</blockquote>
<a name="inta132"></a><p><table cellspacing="0" class="allBorders" border="1" RULES="none" cellpadding="5"><colgroup><col width="500"></colgroup><thead></thead><tr><td class="docTableCell" align="left" valign="top"><p class="docText">
<pre>
#include &lt;unistd.h&gt;

pid_t getsid(pid_t <span class="docEmphItalicAlt">pid</span>);
</pre><br>

</p></td></tr><tr><td class="docTableCell" align="right" valign="top"><p class="docText">Returns: session leader's process group ID if OK, 1 on error</p></td></TR></table></P><br>
<p class="docText">If <span class="docEmphasis">pid</span> is 0, <tt>getsid</tt> returns the process group ID of the calling process's session leader. For security reasons, some implementations may restrict the calling process from obtaining the process group ID of the session leader if <span class="docEmphasis">pid</span> doesn't belong to the same session as the caller.</P>

<UL></UL></td></TR></table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/team.gif" width="60" height="17" border="0" align="absmiddle"  alt="Team BBL"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=ch09lev1sec4.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<a href=ch09lev1sec6.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
</body></html><br>
<table width="100%" cellspacing="0" cellpadding="0"
style="margin-top: 0pt; border-collapse: collapse;"> 
<tr> <td align="right" style="background-color=white; border-top: 1px solid gray;"> 
<a href="http://www.zipghost.com/" target="_blank" style="font-family: Tahoma, Verdana;
 font-size: 11px; text-decoration: none;">The CHM file was converted to HTM by Trial version of <b>ChmD<!--79-->ecompiler</b>.</a>
</TD>
</TR><tr>
<td align="right" style="background-color=white; "> 
<a href="http://www.etextwizard.com/download/cd/cdsetup.exe" target="_blank" style="font-family: Tahoma, Verdana;
 font-size: 11px; text-decoration: none;">Download <b>ChmDec<!--79-->ompiler</b> at: http://www.zipghost.com</a>
</TD></tr></table>
